using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Prota
{
    public static partial class MethodExtensions
    {
        public static string WithoutExt(this string path)
        {
            var i = path.LastIndexOf('.');
            if(i < 0) return path;
            return path[..i];
        }
        
        public static string GetLowerStr(this string s)
        {
            if(s.IsLower()) return s;
            return s.ToLower();
        }
        
        public static bool IsLower(this string s)
        {
            foreach(var c in s) if(!char.IsLower(c)) return false;
            return true;
        }
        
        public static bool NullOrEmpty(this string s) => string.IsNullOrEmpty(s);
        
        public static string Log(this string x)
        {
            Console.WriteLine(x);
            return x;
        }
        
        public static string LogError(this string x)
        {
            Console.Error.WriteLine(x);
            return x;
        }
        
        public static StringBuilder ToStringBuilder(this string x)
        {
            var s = new StringBuilder();
            s.Append(x);
            return s;
        }
        
        [ThreadStatic] static StringBuilder _stringBuilder;
        public static string Join(this IEnumerable<string> list, string separator)
        {
            if(_stringBuilder == null) _stringBuilder = new StringBuilder();
            _stringBuilder.Clear();
            var first = true;
            foreach(var s in list)
            {
                if(first) first = false;
                else _stringBuilder.Append(separator);
                _stringBuilder.Append(s);
            }
            return _stringBuilder.ToString();
        }
        
        public static string ToByteSeqString(this string s)
        {
            return s.Select(x => (int)x).ToStringJoined(",");
        }
        
        public static string WithEncoding(this string s, Encoding src, Encoding dst)
        {
            Console.WriteLine(src.GetBytes(s).Select(x => ((char)x).ToString()).ToStringJoined(":"));
            return dst.GetString(src.GetBytes(s));
        }
        
        public static string GBKToUTF8(this string s)
        {
            return s.WithEncoding(Encoding.GetEncoding("gb2312"), Encoding.UTF8);
        }
        
    }
    
}
